import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 应用状态管理
export const useAppStore = defineStore('app', () => {
  // 状态
  const theme = ref<'light' | 'dark'>('light')
  const language = ref<'zh-CN' | 'en-US'>('zh-CN')
  const loading = ref<boolean>(false)
  const networkStatus = ref<boolean>(true)
  const systemInfo = ref<any>(null)
  const tabBarHeight = ref<number>(0)

  // 计算属性
  const isDark = computed(() => theme.value === 'dark')
  const isOnline = computed(() => networkStatus.value)

  // 切换主题
  const toggleTheme = () => {
    theme.value = theme.value === 'light' ? 'dark' : 'light'
    uni.setStorageSync('theme', theme.value)
  }

  // 设置语言
  const setLanguage = (lang: 'zh-CN' | 'en-US') => {
    language.value = lang
    uni.setStorageSync('language', lang)
  }

  // 设置加载状态
  const setLoading = (status: boolean) => {
    loading.value = status
  }

  // 设置网络状态
  const setNetworkStatus = (status: boolean) => {
    networkStatus.value = status
  }

  // 获取系统信息
  const getSystemInfo = async () => {
    try {
      const info = await uni.getSystemInfo()
      systemInfo.value = info
      return info
    } catch (error) {
      console.error('获取系统信息失败:', error)
      return null
    }
  }

  // 获取安全区域信息
  const getSafeAreaInsets = () => {
    if (!systemInfo.value) return { top: 0, bottom: 0, left: 0, right: 0 }
    
    const { safeArea, screenHeight, screenWidth } = systemInfo.value
    return {
      top: safeArea?.top || 0,
      bottom: screenHeight - (safeArea?.bottom || screenHeight),
      left: safeArea?.left || 0,
      right: screenWidth - (safeArea?.right || screenWidth)
    }
  }

  // 初始化应用配置
  const initApp = () => {
    try {
      // 恢复主题设置
      const storedTheme = uni.getStorageSync('theme')
      if (storedTheme) {
        theme.value = storedTheme
      }

      // 恢复语言设置
      const storedLanguage = uni.getStorageSync('language')
      if (storedLanguage) {
        language.value = storedLanguage
      }

      // 获取系统信息
      getSystemInfo()

      // 监听网络状态
      uni.onNetworkStatusChange((res) => {
        setNetworkStatus(res.isConnected)
      })

      // 监听应用生命周期
      uni.onAppShow(() => {
        console.log('应用显示')
      })

      uni.onAppHide(() => {
        console.log('应用隐藏')
      })
    } catch (error) {
      console.error('初始化应用配置失败:', error)
    }
  }

  // 显示提示信息
  const showToast = (title: string, icon: 'success' | 'error' | 'loading' | 'none' = 'none') => {
    uni.showToast({
      title,
      icon,
      duration: 2000
    })
  }

  // 显示加载中
  const showLoading = (title: string = '加载中...') => {
    uni.showLoading({
      title,
      mask: true
    })
  }

  // 隐藏加载中
  const hideLoading = () => {
    uni.hideLoading()
  }

  // 显示确认对话框
  const showConfirm = (content: string, title: string = '提示') => {
    return new Promise<boolean>((resolve) => {
      uni.showModal({
        title,
        content,
        success: (res) => {
          resolve(res.confirm)
        },
        fail: () => {
          resolve(false)
        }
      })
    })
  }

  return {
    // 状态
    theme,
    language,
    loading,
    networkStatus,
    systemInfo,
    tabBarHeight,
    
    // 计算属性
    isDark,
    isOnline,
    
    // 方法
    toggleTheme,
    setLanguage,
    setLoading,
    setNetworkStatus,
    getSystemInfo,
    getSafeAreaInsets,
    initApp,
    showToast,
    showLoading,
    hideLoading,
    showConfirm
  }
})
